//
//  String+Reguar.swift
//  正则
//
//  Created by app;e on 15/10/28.
//  Copyright © 2015年 world. All rights reserved.
//

import Foundation

extension String{
    
    func hrefLink() ->(link:String,text:String)?{
        //匹配方案
        let pattern = "<a.*?\"(.*?)\".*?>(.*?)</a>"
        do{
            //根据匹配方案 创建 正则对象
            let regular = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators)
            
            // 利用正则对象 开始匹配
            let result = regular.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSRange(location: 0, length: self.characters.count))
            //判断是否找到匹配项
            if result != nil{
                let linkRange = result!.rangeAtIndex(1)
                let textRange = result!.rangeAtIndex(2)
               
                let link = (self as NSString).substringWithRange(linkRange)
                let text = (self as NSString).substringWithRange(textRange)
                
                return (link,text)
            }
            
            return nil
        }catch{
            print(error)
            return nil
        }
    }
}